﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.mainmenu {
    import flash.events.*;
    import flash.geom.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.mainmenu.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;

    public class AbstractShopTabMediator extends Mediator implements IMediator {

        private var GLOBAL_START_POSITION_ITEMS_TOP:Point;
        private const ITEM_WIDTH_PLUS_MARGINLEFT:int = 63;

		[Inject]
        public var tabDisplay:ShopTabDisplay;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var shopModel:ShopModel;
		[Inject]
        public var sunModel:SunModel;
		[Inject]
        public var languageModel:LanguageModel;
        private var _draggingShopItem:Boolean;// = false
        private var _highlightArrow:ClipComponent;

        private static var ONE_SHOT:Boolean = true;

        public function AbstractShopTabMediator(){
            this.GLOBAL_START_POSITION_ITEMS_TOP = new Point(96, 471);
            super();
        }
        override public function onRegister():void{
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_SHOP_TABS, this.enableTabDisplay);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_SHOP_TABS, this.disableTabDisplay);
            eventMap.mapListener(eventDispatcher, ShopEvent.SHOP_ITEM_LOCKED_CHANGE, this.shopItemLockedChange);
            eventMap.mapListener(eventDispatcher, ShopEvent.SHOP_ITEM_AVAILABLE_CHANGE, this.shopItemAvailableChange);
            eventMap.mapListener(eventDispatcher, ShopEvent.ON_ITEM_COUNT_CHANGED, this.onItemCountChanged);
            eventMap.mapListener(eventDispatcher, ShopEvent.ON_ITEM_COUNT_CHANGED_BY_PUTTING_BACK, this.onItemCountChangedByPuttingBack);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_START_DRAGGING_CUSTOMIZE_ITEM, this.onStartDraggingCustomizeItem);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_STOP_DRAGGING_CUSTOMIZE_ITEM, this.onStopDraggingCustomizeItem);
            eventMap.mapListener(eventDispatcher, EventConstants.PLAY_HIGHTLIGHT_ITEM_ANIMATION, this.playHighlightItemAnimation);
            eventMap.mapListener(eventDispatcher, EventConstants.SHOW_ARROW_IN_SHOP, this.highlightShopItemWithArrow);
            eventMap.mapListener(eventDispatcher, EventConstants.HIDE_ARROW_IN_SHOP, this.removeHighlightArrowIfExists);
            eventMap.mapListener(eventDispatcher, EventConstants.INIT_WORLD_COMPLETE, this.initWorldComplete);
        }
        protected function initWorldComplete(_arg1:Event):void{
            eventMap.unmapListener(eventDispatcher, EventConstants.INIT_WORLD_COMPLETE, this.initWorldComplete);
            dispatch(new ShopEvent(ShopEvent.UPDATE_SHOP_ITEMS));
            this.addShopItems();
        }
        protected function addShopItems():void{
            var _local1:ShopItemDisplay;
            var _local3:ShopItemVO;
            var _local2:Vector.<ShopItemDisplay> = this.tabDisplay.getAllItems();
            for each (_local1 in _local2) {
                _local1.removeEventListener(MouseEvent.CLICK, this.item_click);
                _local1.remove();
                _local1 = null;
            };
            for each (_local3 in this.shopModel.shopItems) {
                this.addShopItem(_local3);
            };
        }
        private function highlightShopItemWithArrow(_arg1:InfoEvent):void{
            var _local2:String = _arg1.id;
            var _local3:ShopItemDisplay = this.tabDisplay.getItemByType(_local2);
            if (!_local3){
                return;
            };
            var _local4 = 420;
            var _local5 = 160;
            this._highlightArrow = RepriseUtils.addClipComponent(this.tabDisplay, "highlightArrowDisplay", AssetsLibrary.getMovieClip("ArrowFocus"));
            var _local6:Point = this.getGlobalItemPosition(_local3);
            this._highlightArrow.top = ((_local6.y - _local4) - _local5);
            this._highlightArrow.left = (_local6.x - 5);
        }
        private function removeHighlightArrowIfExists(_arg1:Event=null):void{
            if (this._highlightArrow){
                this._highlightArrow.remove();
                this._highlightArrow = null;
            };
        }
        private function playHighlightItemAnimation(_arg1:InfoEvent):void{
            var _local2:String = _arg1.id;
            this.tabDisplay.highlightIconByType(_local2);
        }
        private function disableTabDisplay(_arg1:Event):void{
            eventMap.unmapListener(eventDispatcher, ShopEvent.ON_ITEM_ADDED, this.addShopItem);
        }
        private function enableTabDisplay(_arg1:Event):void{
            eventMap.mapListener(eventDispatcher, ShopEvent.ON_ITEM_ADDED, this.addShopItem);
        }
        protected function onStartDraggingCustomizeItem(_arg1:Event):void{
            this._draggingShopItem = true;
        }
        protected function onStopDraggingCustomizeItem(_arg1:Event):void{
            this._draggingShopItem = false;
            this.tabDisplay.showBoxButton();
        }
        protected function shopItemLockedChange(_arg1:ShopEvent):void{
            var _local3:ShopItemDisplay;
            var _local2:ShopItemVO = _arg1.shopItemVO;
            if (_local2.category == this.tabDisplay.category){
                _local3 = this.tabDisplay.getItemByType(_local2.type);
                ((_local3) && (_local3.setLocked(_local2.isLocked)));
            };
        }
        private function shopItemAvailableChange(_arg1:ShopEvent):void{
            var _local3:ShopItemDisplay;
            var _local2:ShopItemVO = _arg1.shopItemVO;
            if (_local2.category == this.tabDisplay.category){
                if (_local2.available){
                    this.addShopItem(_local2);
                    _local3 = this.tabDisplay.getItemByType(_local2.type);
                    this.tabDisplay.scrollToShopItem(_local3);
                } else {
                    this.tabDisplay.removeItemByType(_local2.type);
                };
            };
        }
        protected function onItemCountChanged(_arg1:ShopEvent):void{
            this.updateCount(_arg1);
        }
        protected function onItemCountChangedByPuttingBack(_arg1:ShopEvent):void{
            var _local2:ShopItemDisplay = this.updateCount(_arg1);
            if (_local2){
                this.tabDisplay.scrollToShopItem(_local2);
            };
        }
        protected function updateCount(_arg1:ShopEvent):ShopItemDisplay{
            var _local2:ShopItemVO = _arg1.shopItemVO;
            var _local3:ShopItemDisplay = this.tabDisplay.getItemByType(_local2.type);
            if (_local3){
                _local3.count = _local2.count;
            };
            return (_local3);
        }
        protected function addShopItem(_arg1:ShopItemVO, _arg2:Boolean=false):void{
            var _local3:ShopItemDisplay;
            var _local4 = (_arg1.category == this.tabDisplay.category);
            if (((_local4) && (_arg1.available))){
                _local3 = this.createShopItemDisplay(_arg1);
                _local3.addEventListener(MouseEvent.CLICK, this.item_click);
            };
        }
        protected function createShopItemDisplay(_arg1:ShopItemVO):ShopItemDisplay{
            return (this.tabDisplay.createItem(_arg1.type, _arg1.assetURL, _arg1.destination, _arg1.cash, _arg1.coins, _arg1.numberOfItems.length, _arg1.isLocked, _arg1.row));
        }
        protected function item_click(_arg1:MouseEvent):void{
            this.removeHighlightArrowIfExists();
            if (((this.tabDisplay.isScrolling()) || (this._draggingShopItem))){
                return;
            };
            _arg1.stopPropagation();
            var _local2:ShopItemDisplay = ShopItemDisplay(_arg1.currentTarget);
            var _local3:ShopItemVO = this.shopModel.getShopItemByType(_local2.type);
            if (_local3.isLocked){
                if (_local3.isCurrencyCash()){
                    this.sunModel.simpleMessage = this.languageModel.getTextByKey("sun_message_nocashsshop");
                } else {
                    this.sunModel.simpleMessage = this.languageModel.getTextByKey("sun_message_nocoinsshop");
                };
                return;
            };
            var _local4:String = _local3.getNextID();
            _local2.count = _local3.count;
            dispatch(new CustomizeEvent(CustomizeEvent.ITEM_CLICKED, _local4, _local3.type, _local3.destination));
            if (_local4 == null){
                this.tabDisplay.showTrashButton();
            };
        }
        protected function getGlobalItemPosition(_arg1:ShopItemDisplay):Point{
            var _local2:ShopItemVO = this.shopModel.getShopItemByType(_arg1.type);
            var _local3:int = this.tabDisplay.getColumnOfItem(_arg1);
            var _local4:Point = new Point((this.GLOBAL_START_POSITION_ITEMS_TOP.x + (_local3 * this.ITEM_WIDTH_PLUS_MARGINLEFT)), (this.GLOBAL_START_POSITION_ITEMS_TOP.y + ((_local2.row - 1) * this.ITEM_WIDTH_PLUS_MARGINLEFT)));
            return (_local4);
        }

    }
}//package net.wooga.pets.views.mainmenu 
